﻿using Microsoft.EntityFrameworkCore;
using Volo.Abp;
using Volo.Abp.EntityFrameworkCore.Modeling;

namespace ParkManagement.EntityFrameworkCore
{
    /// <summary>
    /// 停哪儿管理数据上下文模型创建扩展类
    /// </summary>
    public static class ParkManagementDbContextModelCreatingExtensions
    {
        public static void ConfigureParkManagement(
            this ModelBuilder modelBuilder)
        {
            Check.NotNull(modelBuilder, nameof(modelBuilder));

            modelBuilder.Entity<Correct>(b =>
            {
                b.ToTable("Corrects");

                b.ConfigureByConvention();

                b.Property(x => x.Name).IsRequired();
                b.Property(x => x.Photo).IsRequired();
                b.Property(x => x.Content).IsRequired();
                b.Property(x => x.SpotCount).IsRequired().HasDefaultValueSql("0");
            });

            modelBuilder.Entity<Lot>(b =>
            {
                b.ToTable("Lots");

                b.ConfigureByConvention();

                b.Property(x => x.ParkName).IsRequired();
                b.Property(x => x.ImgUrl).IsRequired();
                b.Property(x => x.Address).IsRequired();

                // 停车场、停车记录一对多
                b.HasMany(f => f.Records).WithOne(m => m.Lot);
            });

            modelBuilder.Entity<Record>(b =>
            {
                b.ToTable("Records");

                b.ConfigureByConvention();

                b.Property(x => x.ParkNo).IsRequired();
                b.Property(x => x.PlateNumber).IsRequired();
            });

            modelBuilder.Entity<Car>(b =>
            {
                b.ToTable("Cars");

                b.ConfigureByConvention();

                b.HasIndex(x => x.UserId).IsUnique(false);

                // 车辆、里程一对多
                b.HasMany(f => f.Consumptions).WithOne(m => m.Car);
            });

            modelBuilder.Entity<Consumption>(b =>
            {
                b.ToTable("Consumptions");

                b.ConfigureByConvention();
            });

            modelBuilder.Entity<Move>(b =>
            {
                b.ToTable("Moves");

                b.ConfigureByConvention();

                b.Property(x => x.PlateNo).IsRequired();
                b.Property(x => x.Photo).IsRequired();
                b.Property(x => x.IdCard).IsRequired();
                b.Property(x => x.Tel).IsRequired();
                b.Property(x => x.Province).IsRequired();
                b.Property(x => x.City).IsRequired();
                b.Property(x => x.Area).IsRequired();
                b.Property(x => x.Address);
            });
        }
    }
}